package com.titleevaluation.controller;

import com.titleevaluation.entity.UnitApprovalProof;
import com.titleevaluation.service.UnitApprovalProofService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/applications/{applicationId}/unit-approval-proofs")
public class UnitApprovalProofController {

    private final UnitApprovalProofService unitApprovalProofService;

    public UnitApprovalProofController(UnitApprovalProofService unitApprovalProofService) {
        this.unitApprovalProofService = unitApprovalProofService;
    }

    @PostMapping
    public ResponseEntity<UnitApprovalProof> createUnitApprovalProof(@PathVariable Long applicationId, @RequestBody UnitApprovalProof unitApprovalProof) {
        UnitApprovalProof createdUnitApprovalProof = unitApprovalProofService.createUnitApprovalProof(applicationId, unitApprovalProof);
        return ResponseEntity.ok(createdUnitApprovalProof);
    }

    @GetMapping
    public ResponseEntity<List<UnitApprovalProof>> getUnitApprovalProofsByApplicationId(@PathVariable Long applicationId) {
        List<UnitApprovalProof> unitApprovalProofs = unitApprovalProofService.getUnitApprovalProofByApplicationId(applicationId);
        return ResponseEntity.ok(unitApprovalProofs);
    }

    @GetMapping("/{id}")
    public ResponseEntity<UnitApprovalProof> getUnitApprovalProofById(@PathVariable Long id) {
        return unitApprovalProofService.getUnitApprovalProofById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PutMapping("/{id}")
    public ResponseEntity<UnitApprovalProof> updateUnitApprovalProof(@PathVariable Long id, @RequestBody UnitApprovalProof unitApprovalProof) {
        UnitApprovalProof updatedUnitApprovalProof = unitApprovalProofService.updateUnitApprovalProof(id, unitApprovalProof);
        return ResponseEntity.ok(updatedUnitApprovalProof);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUnitApprovalProof(@PathVariable Long id) {
        unitApprovalProofService.deleteUnitApprovalProof(id);
        return ResponseEntity.noContent().build();
    }
}

